// pages/techgoodlist/techgoodlist.js
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');
Page({

     /**
      * 页面的初始数据
      */
     data: {
          inputShowed: false, //是否隐藏搜索栏
          inputVal: "",
          search: false, //判断模糊查询方法
          total: '0',
          startNum: 1, //页数
          shopList: [], // 商铺列表
          current: 1, // 当前页数
          size: 10 // 当前个数
     },
     searchInput: function() {
          var deptName = this.data.inputVal;
          console.log("查询参数：" + deptName);
          this.selectShops(deptName);
     },
     selectShops: function(deptName) {
          var that = this;
          // 经纬度
          var location = wx.getStorageSync('location');
          console.log("----------lat:" + location.latitude)
          util.request(api.shopList, {
               deptName: deptName,
               lat: location.latitude,
               lng: location.longitude,
               current: this.data.current,
               size: this.data.size
          }, 'POST').then(function(res) {
               console.log("-----商铺：" + JSON.stringify(res));
               // 将米转为公里
               res.records.forEach((e) => {
                    if (e.distance && e.distance < 1000)
                         e.distance = e.distance + '米';
                    else if (e.distance > 1000)
                         e.distance = (Math.round(e.distance / 100) / 10).toFixed(1) + "公里";
               });
               that.setData({
                    shopList: res.records
               });
          });
     },
     goGoodDevice: function(e) {
          var deptId = e.currentTarget.id;
          wx.navigateTo({
               url: '/pages/home/deviceOrderList/deviceOrderList?deptId=' + deptId,
          })
     },
     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function(options) {
          var that = this;
          wx.getLocation({
               type: '',
               altitude: true,
               success: function(res) {
                    // var latitude = res.latitude + 0.001276
                    // var longitude = res.longitude + 0.006256
                    wx.setStorageSync('location', res);
                    that.selectShops();
               },
               fail: function(res) {},
               complete: function(res) {},
          })
     },

     /**
      * 生命周期函数--监听页面初次渲染完成
      */
     onReady: function() {},

     /**
      * 生命周期函数--监听页面显示
      */
     onShow: function() {
     },

     /**
      * 生命周期函数--监听页面隐藏
      */
     onHide: function() {

     },

     /**
      * 生命周期函数--监听页面卸载
      */
     onUnload: function() {

     },

     /**
      * 页面相关事件处理函数--监听用户下拉动作
      */
     onPullDownRefresh: function() {
          this.setData({
               current: 1
          });
          this.selectShops();
     },

     /**
      * 页面上拉触底事件的处理函数
      */
     onReachBottom: function() {

     },

     /**
      * 用户点击右上角分享
      */
     onShareAppMessage: function() {

     },
     // 搜索
     showInput: function() {
          this.setData({
               inputShowed: true
          });
     },
     hideInput: function() {
          this.setData({
               inputVal: "",
               inputShowed: false,
               search: false
          });
          this.onLoad();
     },
     clearInput: function() {
          this.setData({
               inputVal: ""
          });
     },
     deptNameInput: function(e) {
          this.setData({
               inputVal: e.detail.value
          })
     }
})